package com.ruoyi.teach.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teach.domain.SchStudent;
import com.ruoyi.teach.service.ISchStudentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生Controller
 * 
 * @author ruoyi
 * @date 2021-12-08
 */
@RestController
@RequestMapping("/teach/student")
public class SchStudentController extends BaseController
{
    @Autowired
    private ISchStudentService schStudentService;

    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('teach:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(SchStudent schStudent)
    {
        startPage();
        List<SchStudent> list = schStudentService.selectSchStudentList(schStudent);
        return getDataTable(list);
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('teach:student:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SchStudent> util = new ExcelUtil<SchStudent>(SchStudent.class);
        List<SchStudent> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = schStudentService.importStudent(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SchStudent> util = new ExcelUtil<SchStudent>(SchStudent.class);
        util.importTemplateExcel(response, "学生数据");
    }


    /**
     * 导出学生列表
     */
    @PreAuthorize("@ss.hasPermi('teach:student:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SchStudent schStudent)
    {
        List<SchStudent> list = schStudentService.selectSchStudentList(schStudent);
        ExcelUtil<SchStudent> util = new ExcelUtil<SchStudent>(SchStudent.class);
        util.exportExcel(response, list, "学生数据");
    }

    /**
     * 获取学生详细信息
     */
    @PreAuthorize("@ss.hasPermi('teach:student:query')")
    @GetMapping(value = "/{sId}")
    public AjaxResult getInfo(@PathVariable("sId") Long sId)
    {
        return AjaxResult.success(schStudentService.selectSchStudentBySId(sId));
    }

    /**
     * 新增学生
     */
    @PreAuthorize("@ss.hasPermi('teach:student:add')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchStudent schStudent)
    {
        return toAjax(schStudentService.insertSchStudent(schStudent));
    }

    /**
     * 修改学生
     */
    @PreAuthorize("@ss.hasPermi('teach:student:edit')")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchStudent schStudent)
    {
        System.out.println(schStudent);
        return toAjax(schStudentService.updateSchStudent(schStudent));
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@ss.hasPermi('teach:student:remove')")
    @Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sIds}")
    public AjaxResult remove(@PathVariable Long[] sIds)
    {
        return toAjax(schStudentService.deleteSchStudentBySIds(sIds));
    }

    /**
     * 查询未分班的学生信息
     */
    @PreAuthorize("@ss.hasPermi('teach:student:fstudent')")
    @Log(title = "学生", businessType = BusinessType.OTHER)
    @GetMapping("fstudent")
    public TableDataInfo fStudent(SchStudent schStudent){
        startPage();
        List<SchStudent> list = schStudentService.selectSchStudentListf(schStudent);
        return getDataTable(list);



    }
}
